
package fr.unice.outils;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Bruno
 */
public class Playlist extends HttpServlet {

    public final static String FS = System.getProperty("file.separator");
    public Playlist() {
        
    }

    @Override
    public void init(ServletConfig config) throws ServletException
  {
    super.init(config);
  }

    @Override
  public void doGet (HttpServletRequest request,
                     HttpServletResponse response)
    throws ServletException, IOException, java.net.MalformedURLException
    {
        try {
        	double random = Math.random(); 
        	String file_name="playlist"+random+".xml";
        	File f = new File(file_name); 
        	FileOutputStream file = new FileOutputStream(f); 
        	String FS = System.getProperty("file.separator");
            String ctx = getServletContext().getRealPath(file_name);

            FileWriter fno = new FileWriter(ctx); 
            BufferedWriter bno = new BufferedWriter(fno);
            String[] liste=request.getParameter("liste_videos").split("~");
            String retour=new String();
            retour="<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\n<playlist version=\"1\" xmlns=\"http://xspf.org/ns/0/\">\n<trackList>";
            for(String s:liste) {
                retour+="<track>\n<title>Titre</title>\n<creator>YouNice</creator>\n<location>http://www.youtube.com/watch?v="+s+"</location>\n<image>xml/capture.png</image>\n</track>\n";
            }
            retour+="</trackList>\n</playlist>";
            byte[] b = new byte[40]; 
            b=retour.getBytes();
            file.write(b);
            fno.write(retour);
            fno.close();
            file.close();
            bno.close();
            PrintWriter out = response.getWriter();
            System.out.println(retour);
            out.println(file_name);
            out.close();
        } catch (IOException e) {

        }

  }
  
}
